// pages/personal/history.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        historyLists: [],
        page: 1,
        showMore: true,
        title:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var cacheUserInfo = wx.getStorageSync('user_phone');
        var that = this;
        // 每次页面打开的时候, 分页先置为1
        that.setData({
            page: 1
        });
        let title = this.data.title
        wx.request({
            url: app.globalData.domain + 'index/index/newUserHistory',
            method: 'post',
            data: {
                user_phone: cacheUserInfo.phoneNumber ? cacheUserInfo.phoneNumber : '',
                title:title
            },
            success(res) {
                for (var i = 0; i < res.data.data.data.length; i++) {
                    that.data.historyLists.push(res.data.data.data[i]);
                }
                that.setData({
                    historyLists: that.data.historyLists
                });
                if (res.data.data.current_page >= res.data.data.last_page) {
                    that.setData({
                        showMore: false
                    });
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    seach(){
        console.log(this.data.title)
        let title = this.data.title
        var that = this;
        var cacheUserInfo = wx.getStorageSync('user_phone');

        wx.request({
            url: app.globalData.domain + 'index/index/newUserHistory',
            method: 'post',
            data: {
                user_phone: cacheUserInfo.phoneNumber ? cacheUserInfo.phoneNumber : '',
                page: 1,
                title:title
            },
            success(res) {
                for (var i = 0; i < res.data.data.data.length; i++) {
                    that.data.historyLists.push(res.data.data.data[i]);
                }
                that.setData({
                    historyLists: res.data.data.data,
                    page:1
                });
                if (res.data.data.current_page >= res.data.data.last_page) {
                    that.setData({
                        showMore: false
                    });
                }
            }
        })

    },
    titleinfo(e){
        // console.log(e)
        this.setData({
            title:e.detail.value
        })
    },
    /**
     * 加载更多
     */
    loadMore: function () {
        var that = this;
        var title = this.data.title;
        var cacheUserInfo = wx.getStorageSync('user_phone');

        wx.request({
            url: app.globalData.domain + 'index/index/newUserHistory',
            method: 'post',
            data: {
                user_phone: cacheUserInfo.phoneNumber ? cacheUserInfo.phoneNumber : '',
                title:title,
                page: that.data.page + 1
            },
            success(res) {
                for (var i = 0; i < res.data.data.data.length; i++) {
                    that.data.historyLists.push(res.data.data.data[i]);
                }
                that.setData({
                    historyLists: that.data.historyLists,
                    page: that.data.page + 1
                });
                // console.log(that.data.page, res.data.data.last_page);
                // 如果当前页码 = 总页数. 隐藏加载更多
                // if (that.data.page >= res.data.data.last_page) {
                //     that.setData({
                //         showMore: false
                //     });
                // }

                if (res.data.data.current_page >= res.data.data.last_page) {
                    that.setData({
                        showMore: false
                    });
                }
                // console.log(that.data.historyLists);
            }
        })
    },
    /**
     * 到结果页
     */
    toResult: function (e) {
        var resId = e.currentTarget.dataset.num_sn;
        var show_times = e.currentTarget.dataset.show_times;
        var personMenu = e.currentTarget.dataset.person_menu;
        var historyId = e.currentTarget.dataset.history_id;
        // 大菜单下面的子菜单
        var personItypes = e.currentTarget.dataset.person_menu_son;
        let index = e.currentTarget.dataset.index
        let result_type = this.data.historyLists[index].result_type
        if (personMenu > 4) {
            wx.navigateTo({
                url: '/pages/result5/index?history_id=' + historyId+'&flag=0&isHidden=2',
            })
        } else {
            var goUri = '/pages/newResults/newResults';
            // 如果是 工作事业选择
            if (personMenu == 2) goUri = '/pages/works/works';
            // 如果是 情感咨询选择
            if (personMenu == 3) goUri = '/pages/emotion/emotion';

            wx.navigateTo({
                // url: '/pages/logs/logs?id='+resId,
                url: goUri + '?id=' + resId + '&show_times=' + show_times + '&types=' + personMenu + '&itypes=' + personItypes + '&history_id=' + historyId+'&flag=0&isHidden=2',
            })
        }
    }
})